home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / tests / syscalls / flock / RCS / flockTest.c,v < prev   
Encoding:
Text File  |  1990-05-24  |  4.7 KB  |  208 lines

  1. head     1.1;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.1
  10. date     90.05.24.10.31.26;  author rab;  state Exp;
  11. branches ;
  12. next     ;
  13.  
  14.  
  15. desc
  16. @@
  17.  
  18.  
  19.  
  20. 1.1
  21. log
  22. @Initial revision
  23. @
  24. text
  25. @/*
  26.  * flockTest.c
  27.  *
  28.  * Test to make sure that the flock system call is working properly.
  29.  * If it works properly, this program will exit silently.  If an error
  30.  * occurs, a diagnostic will be printed to stderr.
  31.  *
  32.  * Copyright 1988 Regents of the University of California
  33.  * Permission to use, copy, modify, and distribute this
  34.  * software and its documentation for any purpose and without
  35.  * fee is hereby granted, provided that the above copyright
  36.  * notice appear in all copies.  The University of California
  37.  * makes no representations about the suitability of this
  38.  * software for any purpose.  It is provided "as is" without
  39.  * express or implied warranty.
  40.  */
  41.  
  42. #ifndef lint
  43. static char rcsid[] = "$Header: /t2/test/src/cmds/flockTest/RCS/flockTest.c,v 1.1 88/11/29 03:53:00 rab Exp $";
  44. #endif
  45.  
  46. #include <stdio.h>
  47. #include <sys/file.h>
  48. #include <sys/wait.h>
  49. #include <signal.h>
  50. #include <string.h>
  51.  
  52. #define SHOULD_SUCCEED  0
  53. #define SHOULD_FAIL     1
  54. #define LOCKFILE        "LOCK"
  55. #ifndef __STDC__
  56. #define const
  57. #define volatile
  58. #endif
  59.  
  60. extern int errno;
  61. static const char *programName;
  62. static int errorFlag;
  63. static volatile int continueSignalFlag;
  64.  
  65. #ifdef __STDC__
  66. void main(int argc, const char **argv);
  67. void doLock(int mode);
  68. void gotContinueSignal(void);
  69. #else
  70. void main();
  71. void doLock();
  72. int gotContinueSignal();
  73. #endif
  74.  
  75. /*
  76.  *---------------------------------------------------------------------------
  77.  *
  78.  * main--
  79.  *
  80.  *      Forks off a child.  The child waits for the parent to send a signal
  81.  *      to proceed.  After the parent locks a file, it forks another child
  82.  *      which immediately exits.  This should not release the lock.  The
  83.  *      parent then signals the first child to proceed.  The first child
  84.  *      attempts to lock the file.  This should fail, since the parent should
  85.  *      still have it locked.
  86.  *
  87.  *  Results:
  88.  *      Zero exit code if no errors.  Non-zero otherwise.
  89.  *
  90.  *  Side effects:
  91.  *      If the program exits abnormally, it may leave the lock file
  92.  *      lying around.
  93.  *
  94.  *---------------------------------------------------------------------------
  95.  */
  96. /*ARGSUSED*/
  97. void
  98. main(argc, argv)
  99.     int argc;
  100.     const char **argv;
  101. {
  102.     union wait waitStatus;
  103.     int childPid;
  104.  
  105.     programName = *argv;
  106.     if ((childPid = fork()) == 0) {
  107.     (void) signal(SIGUSR1, gotContinueSignal);
  108.     while (continueSignalFlag == 0)    /* wait for parent to continue */
  109.         (void) pause();
  110.     doLock(SHOULD_FAIL);
  111.     exit(errorFlag);
  112.     } else {
  113.     doLock(SHOULD_SUCCEED);
  114.     if (fork() == 0)    /* create a second child that immediately dies */
  115.         exit(0);
  116.     (void) wait((union wait *) NULL);   /* wait for 2nd child to die */
  117.     (void) kill(childPid, SIGUSR1);     /* signal 1st child to continue */
  118.     while (wait(&waitStatus) > 0) {     /* wait for first child to die */
  119.         if (waitStatus.w_retcode)       /* check child's exit code */
  120.             ++errorFlag;
  121.     }
  122.     (void) unlink(LOCKFILE);
  123.     exit(errorFlag);
  124.     }
  125. }
  126.  
  127. /*
  128.  *---------------------------------------------------------------------------
  129.  *
  130.  *  doLock --
  131.  *
  132.  *      Opens a file called `LOCK' in the current directory, and then
  133.  *      attempts to lock it.  The input parameter indicates whether the
  134.  *      lock should succeed or fail.
  135.  *
  136.  *  Results:
  137.  *      none.
  138.  *
  139.  *  Side effects:
  140.  *      If the result was other than what was expected a diagnostic is
  141.  *      printed to stderr, and `errorFlag' is set.
  142.  *
  143.  *---------------------------------------------------------------------------
  144.  */
  145.  
  146. void
  147. doLock(mode)
  148.     int mode;
  149. {
  150.     int fd;
  151.  
  152.     if ((fd = open(LOCKFILE, O_RDWR|O_CREAT, 0666)) < 0) {
  153.     (void) fprintf(stderr, "%s: open failed: %s\n",
  154.         programName, strerror(errno));
  155.     ++errorFlag;
  156.     return;
  157.     }
  158.     if (flock(fd, LOCK_EX|LOCK_NB) < 0) {
  159.     if (mode == SHOULD_SUCCEED) {
  160.         (void) fprintf(stderr, "%s: flock failed: %s\n",
  161.         programName, strerror(errno));
  162.         ++errorFlag;
  163.         return;
  164.     }
  165.     } else {
  166.     if (mode == SHOULD_FAIL) {
  167.         (void) fprintf(stderr,
  168.         "%s: flock successful, should have failed.\n", programName);
  169.         ++errorFlag;
  170.         return;
  171.     }
  172.     }
  173.     return;
  174. }
  175.  
  176. /*
  177.  *---------------------------------------------------------------------------
  178.  *
  179.  * gotSignal --
  180.  *      
  181.  *      Called when the child gets a signal.
  182.  *
  183.  * Results:
  184.  *      None.
  185.  *
  186.  * Side effects:
  187.  *
  188.  *      Set a flag that indicates that a signal has been received.
  189.  *
  190.  *
  191.  *---------------------------------------------------------------------------
  192.  */
  193.  
  194. #ifdef __STDC__
  195. void 
  196. #else
  197. int
  198. #endif
  199. gotContinueSignal()
  200. {
  201.     continueSignalFlag = 1;
  202. #ifndef __STDC__
  203.     return 0;
  204. #endif
  205. }
  206.  
  207. @
  208.